import requests

from api import log
from config import DIR_PATH, HOST
from util import read_json


class ApiRegisterLogin:
    # 初始化
    def __init__(self,session):
         # 获取session对象
        self.session = session
        self.__img_code_url = HOST + "/common/public/verifycode1/{}"
        self.__phone_code_url = HOST + "/member/public/sendSms"
        self.__register_url = HOST + "/member/public/reg"
        self.__login_url = HOST + "/member/public/login"
        self.__login_status_url = HOST + "/member/public/islogin"



    # 1、获取图⽚验证码接⼝ 封装
    def api_img_code(self, random):
        log.info("正在调用获取验证码接口，请求方法:{},请求URL:{}".format("get", self.__img_code_url.format(random)))
        return self.session.get(url=self.__img_code_url.format(random))

    # 2、获取短信验证码接⼝ 封装
    def api_phone_code(self, phone, imgVerifyCode):
        data = {
            "phone": phone,
            "imgVerifyCode": imgVerifyCode,
            "type": "reg"
        }
        log.info("正在调用获取短信验证码接口，请求方法：{}, 请求url:{} 请求参数：{}".format("post", self.__phone_code_url, data))
        return self.session.post(url=self.__phone_code_url, data=data)

    # 3、注册接⼝ 封装
    def api_register(self, phone, password, verifycode, phone_code):
        data = {
            "phone": phone,
            "password": password,
            "verifycode": verifycode,
            "phone_code": phone_code
        }
        log.info("正在调用注册接口，请求方法：{} 请求url:{} 请求参数：{}".format("post", self.__register_url, data))
        # 2、调用请求方法
        return self.session.post(url=self.__register_url, data=data)

    # 4、登录接⼝ 封装

    def api_login(self, keywords="18437015028", password="hao123"):
        data={
            "keywords": keywords,
            "password": password
        }
        log.info("正在调用登录接口，请求方法：{} 请求url:{} 请求参数：{}".format("post", self.__login_url, data))
        return self.session.post(self.__login_url, data=data)

    # 5、查询登录状态接⼝ 封装

    def api_login_status(self):
        log.info("正在调用查询登录状态接口，请求方法：{} 请求url：{}".format("post", self.__login_status_url))
        return self.session.post(self.__login_status_url)


if __name__ == '__main__':
   a = ApiRegisterLogin(requests.session())
   print(a.api_img_code(0.235))






